<template>
  <a-modal
    title="编辑博客标签"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false">
          <a-input v-decorator="['tagId']"/>
        </a-form-item>
        <a-form-item
          label="标签名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入标签名称"
            v-decorator="['tagName', {rules: [{required: true, message: '请输入标签名称！'}]}]"/>
        </a-form-item>
        <a-form-item
          label="删除状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group
            placeholder="请选择删除状态"
            v-decorator="['isDeleted',{rules: [{ required: true, message: '请选择删除状态！' }]}]">
            <a-radio
              v-for="(item,index) in isDeletedData"
              :key="index"
              :value="parseInt(item.code)">{{ item.name }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { blogTagEdit } from '@/api/modular/main/blogtag/blogTagManage'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      isDeletedData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    edit(record) {
      this.visible = true
      const isDeletedOption = this.$options
      this.isDeletedData = isDeletedOption.filters['dictData']('dict_is_deleted')
      setTimeout(() => {
        this.form.setFieldsValue(
          {
            tagId: record.tagId,
            tagName: record.tagName,
            isDeleted: record.isDeleted
          }
        )
      }, 100)
    },
    handleSubmit() {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (values[key] && (typeof (values[key]) === 'object')) {
              values[key] = JSON.stringify(values[key])
            }
          }
          blogTagEdit(values).then((res) => {
            if (res.success) {
              this.$message.success('编辑成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error('编辑失败')//  + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
